package scau.redi.social_platform.service.impl;

import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import scau.redi.social_platform.common.util.ResponsePageUtil;
import scau.redi.social_platform.dao.*;
import scau.redi.social_platform.domain.pojo.Shared;
import scau.redi.social_platform.domain.pojo.SharedContent;
import scau.redi.social_platform.domain.pojo.SharedLiked;
import scau.redi.social_platform.domain.pojo.UserInfo;
import scau.redi.social_platform.domain.vo.BriefSharedVO;
import scau.redi.social_platform.service.SearchService;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: Redi
 * @Date: 2022/5/17 21:19
 * @Description:
 */
@Service
public class SearchServiceImpl implements SearchService {

    @Autowired
    SharedMapper sharedMapper;
    @Autowired
    SharedContentMapper sharedContentMapper;
    @Autowired
    SharedImgMapper sharedImgMapper;
    @Autowired
    SharedCollectionMapper sharedCollectionMapper;
    @Autowired
    SharedLikedMapper sharedLikedMapper;
    @Autowired
    SharedCommentMapper sharedCommentMapper;
    @Autowired
    UserInfoMapper userInfoMapper;

    @Override
    public ResponsePageUtil<BriefSharedVO> findByContent(String content, Integer page, Integer size) {
        List<Shared> sharedList = new ArrayList<>();
        PageHelper.startPage(page, size);
        List<SharedContent> sharedContainsContents = sharedContentMapper.selectByContentLike(content);

        for (SharedContent sharedContent:sharedContainsContents){
            Shared shared = sharedMapper.selectByPrimaryKey(sharedContent.getSharedId());
            sharedList.add(shared);
        }

        List<BriefSharedVO> VOList = new ArrayList<>();
        for (Shared shared : sharedList) {
            BriefSharedVO vo = new BriefSharedVO();
            vo.setSharedId(shared.getId());
            vo.setUserId(shared.getUserId());
            vo.setContent(sharedContentMapper.selectBySharedId(shared.getId()).getContent());
            vo.setImgList(sharedImgMapper.selectBySharedId(shared.getId()));
            vo.setCollectedSum(sharedCollectionMapper.selectBySharedId(shared.getId()).size());
            vo.setLikedSum(sharedLikedMapper.selectBySharedId(shared.getId()).size());
            vo.setCommentSum(sharedCommentMapper.selectBySharedId(shared.getId()).size());
            vo.setCreateTime(shared.getCreateTime());
            VOList.add(vo);
        }

        // 把分页的list传入（包含分页信息），后面再把data内容换为VOList
        ResponsePageUtil<SharedContent> sharedWithContentResponsePageUtil = ResponsePageUtil.restPage(sharedContainsContents);

        ResponsePageUtil<BriefSharedVO> pageUtil = new ResponsePageUtil<>(
                page,
                size,
                sharedWithContentResponsePageUtil.getTotalPage(),
                (int) sharedWithContentResponsePageUtil.getTotalItemNum(),
                VOList);
        return pageUtil;
    }

    @Override
    public List<UserInfo> findByUsernameLike(String username, Integer page, Integer size) {
        PageHelper.startPage(page, size);
        List<UserInfo> user = userInfoMapper.selectByUsernameLike(username);

        return user;
    }
}
